package com.im.main;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author gongym
 * @date 2019/1/21 14:37
 */
class DownloadFileWithThreadPool {
  void getFileWithThreadPool(List<String> urlList, String baseSavePath, int poolLength) {
    ExecutorService threadPool = Executors.newCachedThreadPool();
    int length = urlList.size();
    for (int i = 0; i < poolLength; i++) {
      int start = i * length / poolLength;
      int end = (i + 1) * length / poolLength - 1;
      if (i == poolLength - 1) {
        end = length;
      }
      DownloadWithRange download = new DownloadWithRange(urlList, baseSavePath, start, end);
      threadPool.execute(download);
    }
    threadPool.shutdown();
  }
}
